﻿using System.Collections.Generic;
using System.ComponentModel;

namespace TFSCodeReviewHelper
{
    public class AssetInformation : INotifyPropertyChanged
    {
        public string Location { get; set; }

        private int _PreviousChangeset;
        private int _LastChangeset;

        public int PreviousChangeset
        {
            get { return _PreviousChangeset; }
            set
            {
                _PreviousChangeset = value;
                OnPropertyChanged("PreviousChangeset");
            }
        }
        public int LastChangeset
        {
            get { return _LastChangeset; }
            set
            {
                _LastChangeset = value;
                OnPropertyChanged("LastChangeset");
            }
        }

        private List<AssetChange> _Changes;
        public List<AssetChange> Changes
        {
            get { return _Changes; }
            set { _Changes = value; }
        }

        public AssetInformation(List<AssetChange> changes)
        {
            Changes = changes;
            Reset();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged(string propName)
        {
            if (null != PropertyChanged)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        public void OnChangeSelected(object sender, ChangeSelectedArgs e)
        {
            switch (e.AssetChange.DifferenceParticiaption)
            {
                case DifferenceParticiaption.First:
                    PreviousChangeset = e.AssetChange.Number;
                    break;
                case DifferenceParticiaption.Last:
                    LastChangeset = e.AssetChange.Number;
                    break;
                default:
                    break;
            }
        }

        public void Reset()
        {
            bool foundFirst = false;
            foreach (AssetChange change in Changes)
            {
                if (change != null)
                {
                    if (!foundFirst)
                    {
                        PreviousChangeset = change.Number - 1;
                        foundFirst = true;
                    }
                    else
                        LastChangeset = change.Number;

                    change.DifferenceParticiaption = DifferenceParticiaption.None;
                }
            }
        }
    }
}
